package net.ib.mtalk.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.constant.MTalkConstant;
import android.os.Environment;
import android.util.Log;

/**
 * LogWriter는 기본적으로 안드로이드의 Log클래스를 이용한다.
 * 이와 별도로 e, i, w 함수의 경우 파일에 쓰며 파일에 쓰길 원하지 않는다면 d 함수를 사용하면 된다.
 * @author urunimi@infobank.net
 *
 */
public class MTalkLogWriter {
	
	private MTalkLogWriter() {}
	
	/**
	 * Write a verbose log. Dosen't write a log on the file.
	 * @param message
	 */
	public static void v(String message) {
		if( MTalkConstant.LOG_MODE ) {
			message = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\t[" + Thread.currentThread().getId() + "] " + message;
			Log.v(MTalkConstant.TAG, message);
		}
	}
	
	/**
	 * Write a info log. LogWriter write a log on the file.
	 * @param message
	 */
	public static void i(String message) {
		if( MTalkConstant.LOG_MODE ) {
			message = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\t[" + Thread.currentThread().getId() + "] " + message;
			Log.i(MTalkConstant.TAG, message);
			writeLog(message);
		}
	}
	
	/**
	 * Write a debug log. LogWriter doesn't write a log on the file.
	 * @param message
	 */
	public static void d(String message) {
		if( MTalkConstant.LOG_MODE ) {
			message = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\t[" + Thread.currentThread().getId() + "] " + message;
			Log.d(MTalkConstant.TAG, message);
		}
	}
	
	
	/**
	 * Write a warning log. LogWriter write a log on the file.
	 * @param message
	 */
	public static void w(String message) {
		if( MTalkConstant.LOG_MODE ) {
			message = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\t[" + Thread.currentThread().getId() + "] " + message;
			Log.w(MTalkConstant.TAG, message);
			writeLog(message);
		}
	}

	/**
	 * Write a error log. LogWriter write a log on the file.
	 * @param message
	 */
	public static void e(Throwable e) {
		e(getStringFromThrowable(e));
	}
	
	/**
	 * Write a error log. LogWriter write a log on the file.
	 * @param message
	 */
	public static void e(String message) {
		if( MTalkConstant.LOG_MODE  ) {
			message = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "\t[" + Thread.currentThread().getId() + "] " + message;
			Log.e(MTalkConstant.TAG, message);
			writeLog(message);
		}
	}
	
	/**
	 * Write a log on the log file.
	 * @param message
	 */
	static void writeLog(String message) {
		File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), MTalkConstant.TAG);
		if(!root.canWrite())root.mkdirs();
		if (root.canWrite()) {
			File logFile = new File(root, MTalkConstant.TAG + new SimpleDateFormat("yyyy-MM-dd").format(new Date())+ ".log");
			
			writeTextMessage(message, logFile, true);
		}
	}

	public static String getStringFromThrowable(Throwable e) {
		StringWriter sw = new StringWriter();
	    PrintWriter pw = new PrintWriter(sw);
	    e.printStackTrace(pw);
	    
	    return sw.toString();
	}
	
	static String writeTextMessage(String message, File file, boolean isAppend) {
		try {
			FileOutputStream fos = new FileOutputStream(file, isAppend);
			fos.write((message+"\n").getBytes());
			fos.close();
			
		} catch (IOException ioe) {
			Log.e(MTalkConstant.TAG, "Could not write file " + ioe.getMessage());
		}
		return new String("file://" + file.toString());
	}
}
